package com.cee.controller;

import com.cee.common.result.Result;
import com.cee.common.result.ResultCodeEnum;
import com.cee.dto.ExamUserDTO;
import com.cee.dto.ExamUserResultDTO;
import com.cee.dto.ExamUserUpdDTO;
import com.cee.exception.CeeException;
import com.cee.model.ExamUser;
import com.cee.model.OneType;
import com.cee.service.ExamUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/examUser")
public class ExamUserController {

    @Autowired
    private ExamUserService examUserService;

    @PostMapping ("/page")
    public Result getPage(@RequestBody ExamUserDTO examUserDTO){
        PageInfo pageInfo = examUserDTO.getPageInfo();
        Page<OneType> page = PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());

        examUserService.getExamUserList(examUserDTO);
        return Result.ok(new PageInfo<>(page));
    }

    @PostMapping ("/add")
    public Result addExamUser(@RequestBody ExamUser examUser){

        if (examUser == null){
            throw new CeeException(ResultCodeEnum.PARAM_ERROR);
        }

        examUserService.saveExamUser(examUser);

        return Result.ok();
    }


    @GetMapping ("/delete")
    public Result deleteExamUser(Integer id){

        examUserService.deleteExamUserById(id);
        return Result.ok();
    }

    @GetMapping ("/detail")
    public Result getExamById(Integer id){
        ExamUserResultDTO examUser = examUserService.getExamUser(id);
        return Result.ok(examUser);
    }


    @PostMapping ("/update")
    public Result updateExamUser(@RequestBody ExamUserUpdDTO examUserUpdDTO){
        examUserService.updateExamUser(examUserUpdDTO);
        return Result.ok();
    }

}
